package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.yiqidian.ApplayLizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.RuleListResultBean;
import com.eqdd.yiqidian.bean.rebuild.RuleResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.MultiSelectBuilder;
import com.eqdd.yiqidian.ui.select.SelectMultiPostActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiStaffActivity;
import com.eqdd.yiqidian.ui.select.SelectPostActivity;
import com.eqdd.yiqidian.ui.select.SelectPostMultiActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.ListUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ApplyTiaogangActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;
    private ActionSheetDialog xinziDialog;
    @ToSave
    String changer;
    @ToSave
    int postId;
    @ToSave
    int departId;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "调岗申请");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_TIAOGANG)
                        .params("companyId", AppConstent.user.getCompanyId())
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("changer", changer)
                        .params("changeDepartmentId", departId)
                        .params("changePostId", postId)
                        .params("salary", setParam(2))
                        .params("implementTime", setParam(3))
                        .params("reason", setParam(4))
                        .params("userType", AppConstent.user.getIsAdmin())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 0:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;
            case 1:
                Bundle bundle = new Bundle();
                bundle.putInt(Config.COM_ID, AppConstent.user.getCompanyId());
                bundle.putInt(Config.DEPARTMENT_ID, AppConstent.user.getDepartId());
                startActivityForResult(SelectPostActivity.class, bundle, Config.SELECT_POST);
                break;
            case 2:
                if (xinziDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 36, sheetDialog -> {
                        xinziDialog = sheetDialog;
                        xinziDialog.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    xinziDialog.show();
                }
        }
    }

    @Override
    protected void setEditBeans() {
        if (AppConstent.user.getIsAdmin() == 0) {
            datas.add(new SlimEditHintBean("要调动的人", AppConstent.user.getUsername(), 0));
            changer = AppConstent.user.getGuid();
        } else {
            datas.add(new SlimEditEnterBean("*要调动的人", "", 0, true, "请选择").setNeed(true));
        }
        datas.add(new SlimEditEnterBean("*要调职位", "", 1, true, "请选择").setNeed(true));
        datas.add(new SlimEditEnterBean("*薪资等级", "", 2, true, "请选择").setNeed(true));
        datas.add(new SlimEditTimeBean("*调岗生效时间", "", 3, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("*调岗原因", "", 4, false, "请输入").setNeed(true));

    }

    @Override
    protected void loadNetData() {
//        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
//                .params("userGuid", user.getGuid())
//                .params("companyId", user.getCompanyId())
//                .execute(new DialogCallBack<HttpResult<String>>(ApplyTiaogangActivity.this) {
//                    @Override
//                    public void onSuccess(Response<HttpResult<String>> response) {
//                        HttpResult<String> httpResult = response.body();
//                        ToastUtil.showShort(httpResult.getMsg());
//                        if (httpResult.getStatus() == 200) {
//                            slimAdapterEx.getData().get(4).setContent(httpResult.getItems());
//                            slimAdapterEx.notifyItemChanged(4);
//                        }
//                    }
//                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            EmploymentListResultBean staffBean = (EmploymentListResultBean) data.getSerializableExtra(Config.BEAN);
            slimAdapterEx.getDataItem(0).setContent(staffBean.getStaffName());
            slimAdapterEx.notifyItemChanged(0);
            changer = staffBean.getGuid();
            return;
        }
        if (requestCode == Config.SELECT_POST && resultCode == Config.SUCCESS) {
            CareerListResultBean careerBean = (CareerListResultBean) data.getSerializableExtra(Config.BEAN);
            postId = careerBean.getId();
            departId = careerBean.getDepartid();
            slimAdapterEx.getDataItem(1).setContent(String.format("%s/%s", careerBean.getDepartName(), careerBean.getName()));
            slimAdapterEx.notifyItemChanged(1);
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
